package leetcode.周赛.第273场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/26 11:58
 * @description：https://leetcode-cn.com/contest/weekly-contest-273/problems/intervals-between-identical-elements/
 */
public class 相同元素的间隔之和 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.getDistances(new int[]{2, 1, 3, 1, 2, 3, 3})));
    }



    class Solution {
        public long[] getDistances(int[] arr) {
            int n = arr.length;
            HashMap<Integer, long[]> map = new HashMap<>();
            for (int i = 0; i < n; i++) {
                if (!map.containsKey(arr[i])) {
                    long[] nums = {0, 1, i};
                    map.put(arr[i], nums);
                } else {
                    map.get(arr[i])[2] += i;
                    map.get(arr[i])[1]++;
                }
            }
            long[] ans = new long[n];
            for (int i = 0; i < n; i++) {
                long[] nums = map.get(arr[i]);
                ans[i] = nums[2] - i * (nums[1] - 2 * nums[0]);
                nums[0]++;
                nums[2] -= i * 2;
            }
            return ans;
        }
    }
}
